(function($){
    $.fn.bestTable = function(options)  
    {
        var defaults = {
            isDrog : true,
            oddtrBackgroundColor:"#EEE",
            isEffect:true,
            effectBackgroundColor:"#CCCCCC",
            isEditor:true,
            editorBackgroundColor:"#FFFFCC",
            isEditorNewColor:true,
            editorNewColorBackgroundColor : "0099FF"
        };
        var opts = $.extend(defaults, options);
        var moveFlag=false;
        var changeTh=null,
        prevLen = null,
        nextLen = null,
        oldWidth,newWidth,nextOldWidth,nextNewWidth;
		$('body').append("<div id='line' style='display:none; width:1px; position:absolute; border-left:2px solid #ccc; height:200px; '></div>");
        $("body").on("selectstart", function() {return !moveFlag;});
        var newTable= $(this);
        if(opts.isDrog)drog(newTable);

        function drog(table){
            var th = $(table).find('thead tr th');
            var td = $(table).find('tbody tr td');
            $('th').on('mousemove',function(event){
                th=$(this);
                prevLen = th.prevAll('.bui-grid-hd:visible').length;
                nextLen = th.nextAll('.bui-grid-hd:visible').length;
                var pos=th.offset();
                if((event.clientX-pos.left<5&&prevLen>=1)||((th.width()-(event.clientX-pos.left))<5&&nextLen>1)){
                        th.css({'cursor':'col-resize' });
                }
                else{
                        th.css({'cursor':'auto'});
                }
            });
            $('th').on('mousedown',function(event){
                th=$(this);
                prevLen = th.prevAll('.bui-grid-hd:visible').length;
                nextLen = th.nextAll('.bui-grid-hd:visible').length;
                var pos=th.offset();
                if((event.clientX-pos.left<5&&prevLen>=1)||((th.width()-(event.clientX-pos.left))<5&&nextLen>1)){
                    moveFlag=true;
                    var line_height=th.parents('div').parents('div').parents('div').height()-30;
                    $('#line').show().css({
                            'height':line_height,
                            'left':event.clientX,
                            'top':pos.top
                    });
                    if(event.clientX-pos.left<th.width()/2){
                            changeTh=th.prev('.bui-grid-hd:visible');
                    }
                    else{
                            changeTh=th;
                    }
                    oldWidth=changeTh.width();
                    nextOldWidth=changeTh.next('.bui-grid-hd:visible').width();
                }
                if(prevLen<1||nextLen<1){
                    return false;
                }
            });
            $('body').on("mousemove", function(event) {
                var count = 0;
                var tempTh = changeTh;
                while(nextLen-- > 0){
                    if(tempTh.next().is(':visible')){
                        ++count;
                    }
                    tempTh = tempTh.next();
                    if(count==2){
                        break;
                    }
                }
                if (moveFlag == true) {
                    if(event.clientX>tempTh.offset().left-40){
                        $('#line').css({ "left": tempTh.offset().left-40}).show();
                    }
                    if(event.clientX<changeTh.offset().left+40){
                        $('#line').css({ "left": tempTh.offset().left+40 }).show();
                    }
                    if(event.clientX>=changeTh.offset().left+40&&event.clientX<=tempTh.offset().left-40){
                        $('#line').css({ "left": event.clientX }).show();
                    }
                }
            });
            $('body').on('mouseup',function(event){
                if(moveFlag == true){
                    var changeNum=changeTh.index();
                    var newWidth=$('#line').offset().left-changeTh.offset().left;//changeWidth
                    var changeWidth=newWidth-oldWidth;
                    changeTh.width(newWidth-40);
                    changeTh.parents('div').parents('div').parents('div').children('div .bui-grid-body').find('tr').each(function(){
                        $(this).find('td').eq(changeNum).width(newWidth-40);
                    });
                    nextNewWidth=nextOldWidth-changeWidth;
                    changeTh.next('.bui-grid-hd:visible').width(nextNewWidth+40);
                    changeTh.parents('div').parents('div').parents('div').children('div .bui-grid-body').find('tr').each(function(){
                        $(this).find('td').eq(changeNum+1).width(nextNewWidth+40);
                    });
                    $('#line').hide();
                    moveFlag=false;
                    changeTh=null;
                }
            });
            return this;
        }  
    };     
      
})(jQuery); 